import request from "../../../untils/request";
import PubSub from 'pubsub-js';
import moment from "moment";

const appInstance = getApp();
Page({
    data: {
        isPlay: false,
        song: {},
        musicId: '',
        musicLink: '',
        durationTime: '00:00',
        currentTime: '00:00',
        currentWidth: 0,
    },
    onLoad: function (options) {
        // 传过来的options中的musicId在options对象属性上，所以用下面方法把musicId从对象的属性名截取出来
        let b = options;
        let a = Object.keys(b)[0]
        let musicId = a.substring(7)
        // 获取音乐详情
        this.getMusicInfo(musicId)
        this.setData({
            musicId
        })
        // 判断当前页面是否在播放
        if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId === musicId) {
            this.setData({
                isPlay: true
            })
        }

        this.bgmAudioManager = wx.getBackgroundAudioManager();
        // 监听音乐播放状态
        this.bgmAudioManager.onPlay(() => {
            this.changePlayState(true)
            appInstance.globalData.musicId = musicId
        });
        this.bgmAudioManager.onPause(() => {
            this.changePlayState(false)
        });
        this.bgmAudioManager.onStop(() => {
            this.changePlayState(false)
        });

        this.bgmAudioManager.onEnded(()=>{
            PubSub.publish('switchType',"next")
            this.setData({
                currentWidth:0,
                currentTime: '00:00'
            })
        })


        // 监听时间播放进度
        this.bgmAudioManager.onTimeUpdate(() => {
            let currentTime = moment(this.bgmAudioManager.currentTime * 1000).format('mm:ss')
            let currentWidth = this.bgmAudioManager.currentTime / this.bgmAudioManager.duration * 450;
            this.setData({
                currentTime,
                currentWidth
            })
        })
    },
    // 修改播放状态
    changePlayState(isPlay) {
        this.setData({
            isPlay
        })
        appInstance.globalData.isMusicPlay = isPlay;
    },
    //获取音乐详情
    async getMusicInfo(musicId) {
        let songData = await request('/song/detail', {ids: musicId})
        let durationTime = moment(songData.songs[0].dt).format('mm:ss')

        this.setData({
            song: songData.songs[0],
            durationTime
        })
        wx.setNavigationBarTitle({
            title: this.data.song.name
        })
    },

    // 控制音乐播放或暂停
    async musicControl(isPlay, musicId, musicLink) {

        if (isPlay) {
            if (!musicLink) {
                let musicLinkData = await request('/song/url', {id: musicId})
                let musicLink = musicLinkData.data[0].url
                this.setData({
                    musicLink
                })
            }
            this.bgmAudioManager.src = musicLink;
            this.bgmAudioManager.title = this.data.song.name;
        } else {
            this.bgmAudioManager.pause()
        }
    },
    // 点击切换上一首/下一首歌曲
    handleSwitch(event) {
        let type = event.currentTarget.id;
        // 关闭当前音乐
        this.bgmAudioManager.stop();
        // 订阅来自recommendSong的musicId消息，并更新上一首/下一首歌曲
        PubSub.subscribe('musicId', (msg, musicId) => {
            console.log(musicId)
            this.getMusicInfo(musicId)
            this.musicControl(true, musicId)
            PubSub.unsubscribe('musicId')
        })
        // 发消息到recommendSong页面
        PubSub.publish('switchType', type)
    },
// 点击播放暂停的切换
    handleMusicPlay() {
        let isPlay = !this.data.isPlay;
        // this.setData({
        //     isPlay
        // })
        let {musicId, musicLink} = this.data
        this.musicControl(isPlay, musicId, musicLink);
    }
});
